USE [SampleGeographyData]
GO

/****** Object:  StoredProcedure [dbo].[usp_CountiesByDistance]    Script Date: 12/30/2010 14:03:26 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



--
-- Get counties within a radius of a geography
--
CREATE PROCEDURE [dbo].[usp_CountiesByDistance]
	@Box geography,
	@Geo geography, 
	@Distance float(53)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
	SELECT NAME, [STATE], AREA, the_geo
	FROM dbo.uscounties
	WHERE 
		(@Box.STIntersects(the_geo) = 1) AND
		(the_geo.STIntersects(@Geo.STBuffer(@Distance)) = 1)
END





GO

